/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerSelectView.java
 *
 * Created on Apr 1, 2011, 11:49:07 PM
 */

package views;

import controllers.CustomerController;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import models.Customer;

/**
 *
 * @author pelle
 */
public class CustomerSelectView extends AbstractView {

    private CustomerController customerCtr = new CustomerController();
    private ArrayList<Customer> searchCustomers = new ArrayList<Customer>();

    private Object opener = null;
    
    /** Creates new form CustomerSelectView */
    public CustomerSelectView(Object opener) {
        initComponents();
        this.opener = opener;
    }

    public CustomerSelectView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        customerList = new javax.swing.JList();
        cusNr = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cusName = new javax.swing.JTextField();
        selectCustomer = new javax.swing.JButton();
        searchBtn = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Søg efter kunde"));

        customerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(customerList);

        cusNr.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cusNrKeyReleased(evt);
            }
        });

        jLabel1.setText("Kunde nr.:");

        jLabel2.setText("Navn:");

        cusName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cusNameKeyReleased(evt);
            }
        });

        selectCustomer.setText("Vælg kunde");
        selectCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectCustomerActionPerformed(evt);
            }
        });

        searchBtn.setText("Søg");
        searchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cusNr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 59, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cusName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(searchBtn))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, selectCustomer))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(cusNr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(cusName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(searchBtn))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(selectCustomer))
        );

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel3.setText("Find kunde");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selectCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectCustomerActionPerformed
        Customer customer = null;
        boolean success = false;

        int selected = customerList.getSelectedIndex();
        if(selected >= 0) {
            customer = searchCustomers.get(selected);
        }

        if(customer != null) {
            if(opener instanceof OrderEditView) {
                OrderEditView view = (OrderEditView)this.opener;
                success = view.setCustomer(customer);
            } else if(opener instanceof CustomerView) {
                CustomerView view = (CustomerView)this.opener;
                success = view.setCustomer(customer);
            } else if(opener instanceof InvoiceCreateView) {
                InvoiceCreateView view = (InvoiceCreateView)this.opener;
                success = view.setCustomer(customer);
            }

            if(success) {
                dispose();
            }
        } else {
            this.showError("Ingen kunde valgt!");
        }
    }//GEN-LAST:event_selectCustomerActionPerformed

    private void cusNrKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cusNrKeyReleased
        if(evt.getKeyCode() == 10) {
            this.searchCustomers();
        }
    }//GEN-LAST:event_cusNrKeyReleased

    private void cusNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cusNameKeyReleased
        if(evt.getKeyCode() == 10) {
            this.searchCustomers();
        }
    }//GEN-LAST:event_cusNameKeyReleased

    private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBtnActionPerformed
        this.searchCustomers();
    }//GEN-LAST:event_searchBtnActionPerformed

    private void searchCustomers() {
        int customerId = 0;
        String customerName = this.cusName.getText();

        if(this.cusNr.getText().length() > 0) {
            try {
                customerId = Integer.valueOf(this.cusNr.getText());
            } catch(NumberFormatException ex) {}
        }

        this.searchCustomers = this.customerCtr.searchCustomers(customerId, customerName);
        this.updateList();
    }

    private void updateList() {
        DefaultListModel listModel = new DefaultListModel();

        if(searchCustomers.size() > 0) {
            if(searchCustomers != null) {
                for(Customer c : searchCustomers) {
                    listModel.addElement("#" + c.getId() + ": " + c.getName());
                }
            }
        } else {
            this.showError("Ingen kunder fundet på din søgning!");
        }

        customerList.removeAll();
        customerList.setModel(listModel);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cusName;
    private javax.swing.JTextField cusNr;
    private javax.swing.JList customerList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton searchBtn;
    private javax.swing.JButton selectCustomer;
    // End of variables declaration//GEN-END:variables

}
